//
//  Descente.m
//  Kayak
//
//  Created by Francoeur-Raymond, David on 12-05-24.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "Descente.h"
#import "Participant.h"

@implementation Descente

@synthesize estTerminer;
@synthesize arriverTimestamp;
@synthesize departTimestamp;
@synthesize didNotFinish;
@synthesize descenteEnCours;
@synthesize portesManquer;
@synthesize portesToucher;


- (id) init {
	self = [super init];
	if (self != nil) {
		self.departTimestamp = nil;
		self.arriverTimestamp = nil;
		self.portesManquer = 0;
		self.portesToucher = 0;
		self.descenteEnCours = NO;
		self.didNotFinish = NO;
		self.estTerminer = NO;
    }
	return self;
}

-(void) demarrerDescente; {
	self.departTimestamp = [NSDate date];
	self.descenteEnCours = YES;
}
-(NSNumber *) terminerDescente : (bool) didNotFinish{
	self.arriverTimestamp = [NSDate date];
	self.descenteEnCours = NO;
	self.didNotFinish = didNotFinish;
	self.estTerminer = YES;
    
    if (!self.didNotFinish)
    {
        double tempsEcoule = -1 *[departTimestamp timeIntervalSinceDate:arriverTimestamp];
        int penalite = portesManquer * 50 + portesToucher * 2;
        tempsEcoule += penalite;
        return [[NSNumber alloc] initWithDouble:tempsEcoule];
    }
    else {
        return [[NSNumber alloc] initWithDouble:DBL_MAX];
    }
}

-(void) ajouterPorteManquer{
    self.portesManquer++;
}

-(void) ajouterPorteToucher{
    self.portesToucher++;
}


-(NSString*) recupererTempsCalculer{
	//calcul du temps et renvoit en string
	if(descenteEnCours){
		double tempsEcoule = -1 * [departTimestamp timeIntervalSinceDate:[NSDate date]];
		return @"[En descente]";
	}else{
		if(didNotFinish){
			return @"[Non-Terminé]";
		}else if (estTerminer) {
			double tempsEcoule = -1 * [departTimestamp timeIntervalSinceDate:arriverTimestamp];
			int penalite = portesManquer * 50 + portesToucher * 2;
			tempsEcoule += penalite;
			return [NSString stringWithFormat:@"[%f secondes]", tempsEcoule];
		}else {
            return @"[Pas encore fait de descente]";
			NSLog(@"[Pas encore fait de descente]");
		}
	}
	return nil;
}	
@end
